//
//  DYDeviceInfoTool.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.
//

#import "DYDeviceInfoTool.h"
#import <sys/sysctl.h>
#import <mach/mach.h>

#include <sys/param.h>
#include <sys/mount.h>

#import "HGBDiskTool.h"
#import "HGBMemoryTool.h"

@implementation DYDeviceInfoTool

+ (CGFloat)getBatteryQuantity {

    return [[UIDevice currentDevice] batteryLevel];

}

+ (UIDeviceBatteryState)getBatteryStauts {

    return [UIDevice currentDevice].batteryState;

}

+ (NSString *)getTotalMemorySize{
     
    float totalSpace = [HGBDiskTool getTotalDiskSpace]/1000000000.00;
    
//    NSFileManager *fm = [NSFileManager defaultManager];
//    NSDictionary *fat = [fm attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
//    totalSpace = [[fat objectForKey:NSFileSystemSize]longLongValue]/1000000000.00;
    return [NSString stringWithFormat:@"%.f",totalSpace];

}
// 获取当前设备可用内存(单位：MB）
+ (long long)getAvailableMemorySize {

    vm_statistics_data_t vmStats;
    mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
    kern_return_t kernReturn = host_statistics(mach_host_self(),
                                               HOST_VM_INFO,
                                               (host_info_t)&vmStats,
                                               &infoCount);
    if(kernReturn != KERN_SUCCESS) {
        return NSNotFound;
    }
    return ((vm_page_size * vmStats.free_count+vm_page_size*vmStats.inactive_count));

}

//获取当前任务所占用的内存（单位：MB）

+ (double)usedMemory{

    return [HGBDiskTool getUsedDiskSpace]/1000000000.00;
    ///1024.0/1024.0/1024.0;
    
  task_basic_info_data_t taskInfo;
  mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
  kern_return_t kernReturn = task_info(mach_task_self(),
                                      TASK_BASIC_INFO,
                                      (task_info_t)&taskInfo,
                                     &infoCount);

  if(kernReturn != KERN_SUCCESS) {
      return NSNotFound;
  }
  return taskInfo.resident_size / 1024.0 / 1024.0;
    
}

+ (NSString *)fileSizeToString:(unsigned long long)fileSize{

    NSInteger KB = 1024;
    NSInteger MB = KB*KB;
    NSInteger GB = MB*KB;
    if(fileSize <10){
        return @"0 B";
    }else if(fileSize <KB){
        return @"< 1 KB";
    }else if(fileSize <MB){
        return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB];
    }else if(fileSize <GB){
        return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB];
    }else{
        return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB];
    }

}


//返回存储内存占用比例
+ (NSString *)getFreeDiskspaceRate{

    float totalSpace;
    float totalUsedSpace = 0.f;
    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSDictionary *fat = [fm attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    totalSpace = [HGBDiskTool getTotalDiskSpace]/1000000000.00;//[[fat objectForKey:NSFileSystemSize]longLongValue]/1000000000.00;
    totalUsedSpace = [self usedMemory];
    double percent = (double)totalUsedSpace/ (double)totalSpace;
    NSString *freeStr = [NSString stringWithFormat:@"%.f%%",percent*100];
    return freeStr;
}

//返回存储内存占用比例
+ (NSString *)getFreeDiskspaceRate1 {

    float totalSpace;
    float totalUsedSpace = 0.f;
    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSDictionary *fat = [fm attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    totalSpace = [HGBDiskTool getTotalDiskSpace]/1000000000.00;//[[fat objectForKey:NSFileSystemSize]longLongValue]/1000000000.00;
    totalUsedSpace = [self usedMemory];
    double percent = (double)totalUsedSpace/ (double)totalSpace;
    NSString *freeStr = [NSString stringWithFormat:@"%.f",percent*100];
    return freeStr;
}

//返回剩余内存
+ (float)getFreeDiskSpace {

    float totalSpace;
    float totalUsedSpace = 0.f;
    
    NSFileManager *fm = [NSFileManager defaultManager];
    NSDictionary *fat = [fm attributesOfFileSystemForPath:NSHomeDirectory() error:nil];
    totalSpace = [HGBDiskTool getTotalDiskSpace]/1000000000.00;//[[fat objectForKey:NSFileSystemSize]longLongValue]/1000000000.00;
    totalUsedSpace = [self usedMemory];
    float percent = totalSpace - totalUsedSpace;
    return percent;
}


+ (NSString *)notRounding:(float)price afterPoint:(int)position{
    NSDecimalNumberHandler* roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:position raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    NSDecimalNumber *ouncesDecimal;
    NSDecimalNumber *roundedOunces;

    ouncesDecimal = [[NSDecimalNumber alloc] initWithFloat:price];
    roundedOunces = [ouncesDecimal decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
   
    return [NSString stringWithFormat:@"%@",roundedOunces];
}


@end
